package com.study.spring_ai;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.model.function.FunctionCallingOptions;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.function.FunctionToolCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ai")
public class AIController {

    private final AIService aiService;

    public AIController(AIService aiService) {
        this.aiService = aiService;
    }

    @GetMapping("/generate")
    public String generate(@RequestParam(value = "prompt", defaultValue = "Hello") String prompt) {
        return aiService.generate(prompt);
    }

    @Autowired
    @Qualifier("voicechatClient")
    private ChatClient chatClient;

    @GetMapping("/ai")
    Map<String, String> completion(@RequestParam(value = "message", defaultValue = "给我讲个笑话") String message, String voice) {
        return Map.of("completion", this.chatClient
                .prompt()
                .system(sp -> sp.param("voice", voice)).user(message)
//                .toolCallbacks(ToolCallbackProvider.from())
                .tools()
                .advisors()
                .options(FunctionCallingOptions.builder()
                        .withFunctionCallback((FunctionCallback) FunctionToolCallback
                                .builder("generate", () -> "hello")
                                .build())
                        .build())
                .call()
                .content());
    }

    @GetMapping("/date")
    Map<String, String> date() {
        return Map.of("completion", this.chatClient.prompt("What day is tomorrow?").call().content());
    }


    @Autowired
    @Qualifier("aiAssistant")
    ChatClient aiAssistant;

    @GetMapping("/ai/memory")
    Map<String, String> memory(@RequestParam(value = "message", defaultValue = "给我讲个笑话") String message, String voice, String token) {
        PromptTemplate promptTemplate = SystemPromptTemplate.builder().template("你是个友好的机器人，使用 {voice}的说话方式").build();
        Message systemMessage = promptTemplate.createMessage(Map.of("voice", voice));
        return Map.of("completion", this.aiAssistant.prompt().system(systemMessage.getText()).user(message).advisors(advisor -> advisor.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, token)).call().content());
    }



    // Request DTO
    public static class AIRequest {
        private String prompt;

        public AIRequest() {
        }

        public AIRequest(String prompt) {
            this.prompt = prompt;
        }

        public String getPrompt() {
            return prompt;
        }

        public void setPrompt(String prompt) {
            this.prompt = prompt;
        }
    }

    // Response DTO
    public static class AIResponse {
        private String response;
        private boolean error;

        public AIResponse(String response, boolean error) {
            this.response = response;
            this.error = error;
        }

        public String getResponse() {
            return response;
        }

        public void setResponse(String response) {
            this.response = response;
        }

        public boolean isError() {
            return error;
        }

        public void setError(boolean error) {
            this.error = error;
        }
    }
}